package com.zzzzzz.common.page;

import org.apache.commons.lang3.StringUtils;

public class PageStart {
	private Integer start = 1;
	private int offset = -1;
	private String url;
	private String urlEnd;
	
	public static final int DEFAULT_OFFSET = 10;
	
	//-- 返回结果 --//
	private long total = -1;
	
	public PageStart(Integer start, int offset, long total, String url){
		this.start = start;
		this.offset = offset;
		this.total = total;
		this.url = url;
	}
	
	public PageStart(Integer start, int offset, long total, String url, String urlEnd){
		this.start = start;
		this.offset = offset;
		this.total = total;
		this.url = url;
		this.urlEnd = urlEnd;
	}
	
	//-- 分页参数访问函数 --//
	/**
	 * 获得当前页的页号,序号从1开始,默认为1.
	 */
	public Integer getStart() {
		return start;
	}

	/**
	 * 设置当前页的页号,序号从1开始,低于1时自动调整为1.
	 */
	public void setStart(final Integer start) {
		this.start = start;

		if (start < 1) {
			this.start = 1;
		}
	}
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	/**
	 * 获得每页的记录数量, 默认为-1.
	 */
	public int getOffset() {
		return offset;
	}

	/**
	 * 设置每页的记录数量.
	 */
	public void setOffset(final int offset) {
		this.offset = offset;
	}
	
	//-- 访问查询结果函数 --//
	/**
	 * 获得总记录数, 默认值为-1.
	 */
	public long getTotal() {
		return total;
	}

	/**
	 * 设置总记录数.
	 */
	public void setTotal(final long total) {
		this.total = total;
	}

	/**
	 * 根据pageSize与totalCount计算总页数, 默认值为-1.
	 */
	public long getTotalPages() {
		if (total < 0) {
			return -1;
		}

		long count = total / offset;
		if (total % offset > 0) {
			count++;
		}
		return count;
	}

	/**
	 * 是否还有下一页.
	 */
	public boolean hasNext() {
		return start + offset <= total;
	}

	/**
	 * 取得下页的页号, 序号从1开始.
	 * 当前页为尾页时仍返回尾页序号.
	 */
	public int getNextStart() {
		if (hasNext()) {
			return start + offset;
		} else {
			return start;
		}
	}

	/**
	 * 是否还有上一页.
	 */
	public boolean hasPre() {
		return start+1 > offset;
	}

	/**
	 * 取得上页的页号, 序号从1开始.
	 * 当前页为首页时返回首页序号.
	 */
	public int getPreStart() {
		if (hasPre()) {
			return start - offset;
		} else {
			return 1;
		}
	}

	public String getUrlEnd() {
		return urlEnd == null ? "" : urlEnd;
	}

	public void setUrlEnd(String urlEnd) {
		this.urlEnd = urlEnd;
	}

	public boolean hasQuesMark(){
		if(StringUtils.isNotBlank(url)) return url.contains("?");
		return false;
	}
	
	public String getPageBar() {
		String url = getUrl();
		url = hasQuesMark()?url+"&start=":url+"?start=";
		StringBuilder sb = new StringBuilder("<ul class='pager'>");
		if(hasPre()){
			sb.append("<li><a href='").append(url).append(getPreStart()).append(getUrlEnd()).append("' class='bottonnext'>上一页</a></li>");
		}
		if(hasNext()){
			sb.append("<li><a href='").append(url).append(getNextStart()).append(getUrlEnd()).append("' class='bottonnext'>下一页</a></li>");
		}
		sb.append("</ul>");
		return sb.toString();
	}
	
	
	
//	// 页面分页导航的链接 方式二
//	public String getPageToolBar2(PageBean pageBean) {
//		String str = "";
//		int pageSplit = (pageNo / 5) * 5;
//		for (int i = pageSplit - 1; i < (pageSplit + 6); i++) {
//			if (i <= 0) {
//			} else if (pageNo == i) {
//				str += i + "&nbsp;";
//			} else if (i > getTotalPages()) {
//			} else {
//				str += "<a href='" + "&pageNo=" + i + "'>" + i + "</a>" + "&nbsp;";
//			}
//		}
//		return str;
//	}
}
